package com.eqdd.yiqidian.ui.mine.wo;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.CollectionGroupBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.EditTextActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ChooseAlertView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * @author吕志豪 .
 * @date 17-11-1  下午4:14.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class SelectCollectionGroupActivity extends BaseActivity {

    private RecyclerViewCustom dataBinding;
    private SlimAdapterEx<CollectionGroupBean> slimAdapterEx;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "分组管理");
        initTopRightText("添加分组", v -> {
            Bundle bundle = new Bundle();
            bundle.putString("title", "新的分组名称");
            bundle.putString("tip", "");
            bundle.putString("content", "");
            startActivityForResult(EditTextActivity.class, bundle, Config.EDIT_TEXT);
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Config.EDIT_TEXT && resultCode == Config.INTENT_CONTENT) {
            String content = data.getStringExtra("content");
            OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.CREATE_COLLECTION_GROUP)
                    .params("owner", user.getGuid())
                    .params("name", content)
                    .execute(new DialogCallBack<HttpResult>(SelectCollectionGroupActivity.this) {
                        @Override
                        public void onSuccess(Response<HttpResult> response) {
                            HttpResult httpResult = response.body();
                            ToastUtil.showShort(httpResult.getMsg());
                            if (httpResult.getStatus() == 200) {
                                refresh();
                            }
                        }
                    });
        } else if (resultCode == Config.INTENT_CONTENT) {
            updateName(requestCode, data.getStringExtra("content"));
        }
    }

    private void updateName(int requestCode, String content) {
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.UPDATE_COLLECTION_GROUP_NAME)
                .params("owner", user.getGuid())
                .params("groupid", slimAdapterEx.getDataItem(requestCode).getId())
                .params("name", content)
                .execute(new DialogCallBack<HttpResult>(SelectCollectionGroupActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            refresh();
                        }
                    }
                });
    }

    @Override
    public void initData() {

        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_29, new SlimInjector<CollectionGroupBean>() {
            @Override
            public void onInject(CollectionGroupBean data, IViewInjector injector) {

                injector.text(R.id.tv_left_content, data.getName())
                        .text(R.id.tv_right_content, data.getCount() + "条收藏")
                        .textColor(R.id.tv_right_content, R.color.hint_color);
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
                    @Override
                    public void onItemClicked(RecyclerView recyclerView, int position, View v) {

                        Intent intent = new Intent();
                        intent.putExtra("id", slimAdapterEx.getDataItem(position).getId());
                        intent.putExtra("name", slimAdapterEx.getDataItem(position).getName());
                        setResult(Config.SUCCESS, intent);
                        finish();
                    }
                })
                .setOnItemLongClickListener((recyclerView, position, v) -> {

                    ChooseAlertView.getBottomChoose(this, null, new String[]{"修改名称", "删除分组"}, (o, pos) -> {

                        if (pos == 1) {
                            Bundle bundle = new Bundle();
                            bundle.putString("title", "修改分组名称");
                            bundle.putString("tip", "");
                            bundle.putString("content", slimAdapterEx.getDataItem(position).getName());
                            startActivityForResult(EditTextActivity.class, bundle, position);
                        } else if (pos == 2) {
                            deleteGroup(slimAdapterEx.getDataItem(position).getId());
                        }
                    }).show();

                    return true;
                });
    }

    private void deleteGroup(int id) {
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.DELETE_COLLECTION_GROUP)
                .params("owner", user.getGuid())
                .params("groupid", id)
                .execute(new DialogCallBack<HttpResult>(SelectCollectionGroupActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            refresh();
                        }
                    }
                });
    }

    @Override
    public void setView() {

        refresh();
    }

    private void refresh() {
        OkGo.<HttpResult<List<CollectionGroupBean>>>post(HttpConfig.BASE_URL + HttpConfig.COLLECTION_GROUP_LIST)
                .params("owner", user.getGuid())
                .execute(new DialogCallBack<HttpResult<List<CollectionGroupBean>>>(SelectCollectionGroupActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<CollectionGroupBean>>> response) {
                        HttpResult<List<CollectionGroupBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {

                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                    }
                });
    }
}
